﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PrinterBudi.Persistence;

namespace PrinterBudi.Desktop
{
    public partial class FormPBEmployee : Form
    {
        private PBEmployee employeeNow;
        private PBEmployee employeeActive;

        public FormPBEmployee(PBEmployee eNow)
        {
            employeeActive = eNow;
            InitializeComponent();
            EditModeEmployee(false);
        }

        public FormPBEmployee()
        {
            InitializeComponent();
            EditModeEmployee(false);
        }

        private void LoadcbPhoneNumberEmployeeItems()
        {
            List<PBEmployee> listemployee = PBEmployeeModel.ReadAllEmployee();

            cbPhoneNumberEmployee.Items.Clear();
            foreach (PBEmployee employee in listemployee)
                cbPhoneNumberEmployee.Items.Add(employee.PhoneNumber);

            cbPhoneNumberEmployee.SelectedIndex = 0;
        }

        private void FormPBEmployee_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'printerBudiDataSet.PBUser' table. You can move, or remove it, as needed.
            //this.pBUserTableAdapter.Fill(this.printerBudiDataSet.PBUser);
            LoadcbPhoneNumberEmployeeItems();
        }

        private void cbPhoneNumberEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            employeeNow = PBEmployeeModel.Read(cbPhoneNumberEmployee.Text);

            tbNameEmployee.Text = employeeNow.Name;
            tbPasswordEmployee.Text = employeeNow.Password;
        }

        private void EditModeEmployee(bool status)
        {
            tbNameEmployee.Enabled = status;
            tbPasswordEmployee.Enabled = status;
            btnEmployeeOK.Visible = status;
            btnEmployeeCancel.Visible = status;

            btnEditEmployee.Visible = !status;
            btnAddEmployee.Visible = !status;
            btnDeleteEmployee.Visible = !status;
            btnViewEmployee.Visible = !status;
            btnClose.Visible = !status;

        }

      
        private void btnAddEmployee_Click(object sender, EventArgs e)
        {
            FormAddEmployee frm = new FormAddEmployee();

            frm.ShowDialog();
            LoadcbPhoneNumberEmployeeItems();
        }

        private void btnViewEmployee_Click(object sender, EventArgs e)
        {
            FormViewPBEmployee frm = new FormViewPBEmployee();

            this.Hide();
            frm.ShowDialog();

            this.Show();
            this.Activate();
            LoadcbPhoneNumberEmployeeItems();
        }

        private void btnEmployeeOK_click(object sender, EventArgs e)
        {
            employeeNow.Name = tbNameEmployee.Text;
            employeeNow.Password = tbPasswordEmployee.Text;

            PBEmployeeModel.Update(employeeNow);
            LoadcbPhoneNumberEmployeeItems();
            EditModeEmployee(false);
        }

        private void btnCancelEmployee_Click(object sender, EventArgs e)
        {
            LoadcbPhoneNumberEmployeeItems();
            EditModeEmployee(false);
        }

        private void btnDeleteEmployee_Click(object sender, EventArgs e)
        {
            if (employeeActive.PhoneNumber == employeeNow.PhoneNumber)
                MessageBox.Show("Can't delete yourself!");
            else
                PBEmployeeModel.Delete(employeeNow);
            LoadcbPhoneNumberEmployeeItems();
        }

        private void btnEditEmployee_Click(object sender, EventArgs e)
        {
            EditModeEmployee(true);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
    }
}
